package org.chim.springcloud.eurekaserviceprovider.controller;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * Class Name: HelloController
 * Create Date: 18-3-6 上午11:41
 * Creator: Chim·Zigui
 * Version: v1.0
 * Updater:
 * Date Time:
 * Description:
 */
@RestController
public class HelloController {

    @Value(value = "${server.port}")
    private Integer port = null;

    @RequestMapping(value = "/hello/{name}")
    @HystrixCommand(fallbackMethod = "helloDefault")
    @ResponseBody
    public Map<String, Object> hello(@PathVariable(value = "name", required = true) String name,
                                     @RequestParam(value = "ext", required = false) String ext,
                                     HttpServletRequest request) {
        Map<String, Object> result = new HashMap<>();
        result.put("code", 0);
        result.put("msg", "success");
        result.put("data", name);
        result.put("change", "服务提供者PORT：" + port);
        result.put("provider-header", request.getHeader("x-label"));
        result.put("provider-params", request.getParameterMap());
        result.put("provider-ext-params", ext);
        return result;
    }

    public Map<String, Object> helloDefault(@PathVariable(value = "name", required = true) String name,
                                            @RequestParam(value = "ext", required = false) String ext,
                                            HttpServletRequest request) {
        Map<String, Object> result = new HashMap<>();
        result.put("code", 0);
        result.put("msg", "success");
        result.put("data", name);
        result.put("change", "Hystrix默认返回");
        result.put("provider-header", request.getHeader("x-label"));
        result.put("provider-ext-params", ext);
        return result;
    }
}
